<?php
namespace app\member\model;

use app\BaseModel;
use think\facade\Db;

class Rights extends BaseModel
{
    //会员权益表
    protected $name = 'longbing_card_member_rights';


    protected $append = [

        'status_text'

    ];


    /**
     * @author chenniang
     * @DataTime: 2020-07-16 11:51
     * @功能说明:状态
     */
    public function getStatusTextAttr($value,$data){

        if(!empty($data['id'])){

            $relation_model = new RightsRelation();

            $info = $relation_model->where(['rights_id'=>$data['id']])->find();

            return !empty($info)?1:0;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-07-15 09:49
     * @功能说明:添加
     */
    public function rightsAdd($data){

        $data['status']      = 1;

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-16 11:57
     * @功能说明:初始化
     */
    public function initData($uniacid){

        $dis = [

            'uniacid' => $uniacid,

            'type'    => 0

        ];

        $data = $this->rightsInfo($dis);

        if(empty($data)){

            $arr = [

                [
                    'title'      => '商品打折',

                    'show_title' =>  '优享会员价',

                    'type'       => 0,

                    'content'    => '',

                    'top'        => 10,

                    'icon'       => 'http://lbqny.migugu.com/admin/member/discount.png',

                    'uniacid'    => $uniacid,

                    'use_type'   => 1,

                    'status'     => 1

                ],[
                    'title'      => '积分倍率',

                    'show_title' =>  '会员多积分',

                    'type'       => 0,

                    'content'    => '',

                    'top'        => 10,

                    'icon'       => 'http://lbqny.migugu.com/admin/member/multiple.jpeg',

                    'uniacid'    => $uniacid,

                    'use_type'   => 2,

                    'status'     => 1
                ],[
                    'title'      => '赠送积分',

                    'show_title' =>  '会员专享积分',

                    'type'       => 0,

                    'content'    => '',

                    'top'        => 10,

                    'icon'       => 'http://lbqny.migugu.com/admin/member/integral.jpeg',

                    'uniacid'    => $uniacid,

                    'use_type'   => 3,

                    'status'     => 1,

                    'is_up'      => 1

                ],
//                [
//                    'title'      => '分销收益率',
//
//                    'show_title' =>  '分销收益',
//
//                    'type'       => 0,
//
//                    'content'    => '',
//
//                    'top'        => 10,
//
//                    'icon'       => '',
//
//                    'uniacid'    => $uniacid,
//
//                    'use_type'   => 5,
//
//                    'status'     => 1
//                ]
            ];

            $this->saveAll($arr);

        }

        $coupon_rights = $this->rightsInfo(['use_type'=>4,'is_up'=>1,'type'=>0,'uniacid'=>$uniacid]);

        if(empty($coupon_rights)){

            $insert = [

                'title'      => '会员送券',

                'show_title' =>  '送卡券',

                'type'       => 0,

                'content'    => '',

                'top'        => 10,

                'icon'       => 'http://lbqny.migugu.com/admin/member/integral.jpeg',

                'uniacid'    => $uniacid,

                'use_type'   => 4,

                'status'     => 1,

                'is_up'      => 1
            ];

            $this->insert($insert);

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:12
     * @功能说明:列表
     */
    public function rightsList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;
    }




    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:08
     * @功能说明:详情
     */
    public function rightsInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:13
     * @功能说明:编辑
     */
    public function rightsUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-22 16:58
     * @功能说明:自定义权益
     */
    public function  customRights($member_id){

        $dis[]= ['b.member_id','=',$member_id];

        $dis[]= ['b.type','=',1];

        $dis[]= ['a.status','=',1];

        $data = $this->alias('a')
                ->join('longbing_card_member_rights_relation b','a.id = b.rights_id')
                ->where($dis)
                ->group('b.rights_id')
                ->column('a.title');

        return array_values($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-28 10:38
     * @功能说明:会员权益
     */
    public function memberRights($level_id,$uniacid){

        $dis = [

            'b.member_id' => $level_id,

            'b.uniacid'   => $uniacid,

            'a.status'    => 1
        ];

        $data = $this->alias('a')
                ->join('longbing_card_member_rights_relation b','a.id = b.rights_id')
                ->where($dis)
                ->field(['a.id','a.show_title','a.type','a.use_type','a.icon'])
                ->group('a.id')
                ->order(['a.top desc','a.id desc'])
                ->select()
                ->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-29 10:49
     * @功能说明:商品等级下拉框
     */
    public function goodsLevelSelect($dis){

        $data = $this->alias('a')
                ->join('longbing_card_member_rights_relation b','a.id = b.rights_id')
                ->join('longbing_card_member_level c','c.id = b.member_id')
                ->where($dis)
                ->field('c.title,c.id,c.top')
                ->group('c.id')
                ->order('c.top')
                ->select()
                ->toArray();

        return $data;
    }







}